package com.easy.work.function.me;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.base.syslib.Logs;
import com.base.syslib.pulltorefresh.PullToRefreshBase;
import com.base.syslib.pulltorefresh.PullToRefreshListView;
import com.base.syslib.util.DensityUtil;
import com.easy.work.R;
import com.easy.work.app.AppSession;
import com.easy.work.app.BaseActivity;
import com.easy.work.bean.WorkBaseInfo;
import com.easy.work.bean.WorkListBean;
import com.easy.work.function.main.WorkBaseDetailAct;
import com.easy.work.function.main.adapter.MainListAdapter;
import com.easy.work.function.me.adapter.MyWorkListAdapter;
import com.easy.work.http.BaseResponse;
import com.easy.work.http.HttpClient;
import com.easy.work.http.request.WorkBaseListInfoRequest;
import com.easy.work.http.request.WorkCollectListRequest;
import com.easy.work.http.response.WorkBaseListInfoList;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2017/12/31.
 * 我的收藏
 */
public class MyCollectAct extends BaseActivity implements PullToRefreshBase.OnRefreshListener<ListView> {
    public static void start(Context ctx) {
        Intent intent = new Intent(ctx, MyCollectAct.class);
        ctx.startActivity(intent);
    }

    PullToRefreshListView refreshListView;
    ListView listView;
    private int status = 0;
    private int page = 0;
    /**
     * 招聘列表
     */
    List<WorkBaseInfo> workBaseInfoList = new ArrayList<>();
    MainListAdapter adapter;
    /**
     * 暂无记录
     */
    private ImageView imgNoData;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_my_collect);
        refreshListView = (PullToRefreshListView) findViewById(R.id.listView);
        imgNoData = (ImageView) findViewById(R.id.img_no_data);
        initListView();
        initTitle("我的收藏");
    }


    /**
     * ListView初始化
     */
    private void initListView() {
        refreshListView.setPullLoadEnabled(true);
        refreshListView.setPullRefreshEnabled(true);
        refreshListView.setOnRefreshListener(this);


        listView = refreshListView.getRefreshableView();
        listView.setHeaderDividersEnabled(false);
        listView.setDividerHeight(0);


//        listView = refreshListView.getRefreshableView();
//        listView.setDivider(new ColorDrawable(0xffF5F5F5));
//        listView.setDividerHeight(DensityUtil.dip2px(this, 10));

        adapter = new MainListAdapter(oThis, workBaseInfoList);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                WorkBaseInfo baseInfo = (WorkBaseInfo) parent.getAdapter().getItem(position);
                WorkBaseDetailAct.start(oThis, baseInfo.workId);
            }
        });

        AppSession.setPullFormartRefreshTime(refreshListView);
        refreshListView.doPullRefreshing(true, 0);
    }

//    /**
//     * 加载位置坐标
//     */
//    private int currentPage = 0;

    /**
     * 获取招聘信息列表
     */
    private void getCollectList() {
        final WorkCollectListRequest request = new WorkCollectListRequest();
        request.userId = AppSession.getInstance().getUserId();
        request.start = adapter.getCount();
        request.limit = 8;
        HttpClient.post(request, new HttpClient.onSuccessListener() {
            @Override
            public void onSuccess(BaseResponse response) {

                WorkBaseListInfoList recommendList = (WorkBaseListInfoList) response;
                if (recommendList != null && recommendList.list != null && recommendList.list.size() > 0) {

                    List<WorkBaseInfo> addList = recommendList.list;
                    workBaseInfoList.addAll(addList);
                }
                adapter.notifyDataSetChanged();
                if (adapter.getCount() < 1) {
                    imgNoData.setVisibility(View.VISIBLE);
                } else {
                    imgNoData.setVisibility(View.GONE);
                }

                refreshListView.onPullUpRefreshComplete();
                refreshListView.onPullDownRefreshComplete();

            }
        }, new HttpClient.onFailListener() {
            @Override
            public void onFail(int code, String msg) {
                Logs.d("MainFragment", "WorkBaseListInfoRequest    code" + code + "      msg=" + msg);
                refreshListView.onPullUpRefreshComplete();
                refreshListView.onPullDownRefreshComplete();
            }
        });

    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
        workBaseInfoList.clear();
        getCollectList();

    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
        getCollectList();
    }
}
